# 病人信息管理的视图文件
from django.shortcuts import render, redirect
from common.models import Patientinfo

def index(request):
    '''浏览信息'''
    umod = Patientinfo.objects
    ulist = umod.all()
    context = {"patientinfolist": ulist}
    return render(request, "myapp/Patientinfo/index.html", context)

def search(request):
    '''查找信息'''
    hspid = request.POST['hspid']
    obj = Patientinfo.objects.get(hspid=hspid)
    context = {'Patientinfo': obj}
    return render(request, "myapp/Patientinfo/edit.html",context)


def add(request):
    '''加载信息添加表单'''
    if request.method == "GET":
        return render(request, "myapp/Patientinfo/add.html")
    # 获取用户POST提交过来的数据
    request.POST.get("hspid", "name", "gender", "departname", "doctorname", "bedid", "inhsptimes", "inhspdiagnose",
                     "departzone", "mrid", "idcardno", "medinsurancetype", "medinsuranceid", "inhsptype", "illness",
                     "illseason", "marriage", "nation", "profession", "address", "corporation", "contactsname",
                     "relation", "contactsphone", "postcode", "birthday", "hsptime")
    # 保存到数据库
    Patientinfo.objects.create()

    return redirect("Patientinfo/")


def insert(request):
    '''执行信息添加'''
    try:
        obj = Patientinfo()
        # 从表单中获取要添加的信息并封装到obj对象中
        obj.hspid = request.POST['hspid']
        obj.name = request.POST['name']
        obj.gender = request.POST['gender']
        obj.departname = request.POST['departname']
        obj.doctorname = request.POST['doctorname']
        obj.bedid = request.POST['bedid']
        obj.inhsptimes = request.POST['inhsptimes']
        obj.inhspdiagnose = request.POST['inhspdiagnose']
        obj.departzone = request.POST['departzone']
        obj.mrid = request.POST['mrid']
        obj.idcardno = request.POST['idcardno']
        obj.medinsurancetype = request.POST['medinsurancetype']
        obj.medinsuranceid = request.POST['medinsuranceid']
        obj.inhsptype = request.POST['inhsptype']
        obj.illness = request.POST['illness']
        obj.illseason = request.POST.get('illseason')
        obj.marriage = request.POST['marriage']
        obj.nation = request.POST['nation']
        obj.profession = request.POST['profession']
        obj.address = request.POST['address']
        obj.corporation = request.POST['corporation']
        obj.contactsname = request.POST['contactsname']
        obj.relation = request.POST['relation']
        obj.contactsphone = request.POST['contactsphone']
        obj.postcode = request.POST['postcode']
        obj.birthday = request.POST['birthday']
        obj.hsptime = request.POST.get('hsptime')
        obj.save()
        context = {'info': "添加成功！"}
    except Exception as err:
        print(err)
        context = {'info': "添加失败！"}
    return render(request, "myapp/info.html", context)


def delete(request,nid):
    try:
        obj = Patientinfo.objects.get(hspid=nid)
        obj.delete()
        context = {'info':"删除成功"}
    except Exception as err:
        print(err)
        context = {'info':"删除失败"}
    return render(request,"myapp/info.html",context)



def edit(request,hspid):
    '''加载信息编辑表单'''
    try:
        obj = Patientinfo.objects.get(hspid=hspid)
        context = {'Patientinfo':obj}
        return render(request, "myapp/Patientinfo/edit.html",context)
    except Exception as err:
        print(err)
        context = {'info':"没有找到要修改的信息！"}
        return render(request,"myapp/info.html",context)

def update(request):
    '''执行信息编辑'''
    try:
        hspid = request.POST['hspid']
        obj = Patientinfo.objects.get(hspid=hspid)
        obj.name = request.POST['name']
        obj.gender = request.POST['gender']
        obj.departname = request.POST['departname']
        obj.doctorname = request.POST['doctorname']
        obj.bedid = request.POST['bedid']
        obj.inhsptimes = request.POST['inhsptimes']
        obj.inhspdiagnose = request.POST['inhspdiagnose']
        obj.departzone = request.POST['departzone']
        obj.mrid = request.POST['mrid']
        obj.idcardno = request.POST['idcardno']
        obj.medinsurancetype = request.POST['medinsurancetype']
        obj.medinsuranceid = request.POST['medinsuranceid']
        obj.inhsptype = request.POST['inhsptype']
        obj.illness = request.POST['illness']
        obj.illseason = request.POST.get('illseason')
        obj.marriage = request.POST['marriage']
        obj.nation = request.POST['nation']
        obj.profession = request.POST['profession']
        obj.address = request.POST['address']
        obj.corporation = request.POST['corporation']
        obj.contactsname = request.POST['contactsname']
        obj.relation = request.POST['relation']
        obj.contactsphone = request.POST['contactsphone']
        obj.postcode = request.POST['postcode']
        obj.birthday = request.POST['birthday']
        obj.hsptime = request.POST.get('hsptime')
        obj.save()
        context = {'info':"修改成功！"}
    except Exception as err:
        print(err)
        context = {'info':"修改失败！"}
    return render(request,"myapp/info.html",context)

